package com.startup;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
//import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Controller;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.im.common.interceptor.DataInterceptor;
import com.im.common.interceptor.SessionInterceptor;
import com.only.common.spring.web.resolver.DataResolver;
import com.only.common.spring.web.resolver.DefineResolver;
import com.only.common.spring.web.resolver.RequestResolver;

/**
 *
 * WebConfig WebServlet配置类 spring mvc 配置
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.im", useDefaultFilters = false, includeFilters = {
    @ComponentScan.Filter(value = {Controller.class})})
@PropertySource({"classpath:config/path.properties"})
public class WebConfig extends WebMvcConfigurerAdapter {

//    @Autowired
//    private Environment environment;

    @Bean
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter(MappingJackson2HttpMessageConverter messageConverter) {
        List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>();
        resolvers.add(dataResolver());
        resolvers.add(requestResolver());
        resolvers.add(defineResolver());
        RequestMappingHandlerAdapter bean = new RequestMappingHandlerAdapter();
        bean.setSynchronizeOnSession(true);
        bean.setCustomArgumentResolvers(resolvers);
        // bean.setWebBindingInitializer(webBindingInitializer());
        bean.getMessageConverters().add(messageConverter);
        return bean;
    }

    @Bean
    public RequestResolver requestResolver() {
        return new RequestResolver();
    }

    @Bean
    public DefineResolver defineResolver() {
        return new DefineResolver();
    }
    
    @Bean
    public DataResolver dataResolver() {
        return new DataResolver();
    }

    /**
     * 视图层解决方案
     *
     * @return
     */
    @Bean
    public ViewResolver viewResolver() {
        Map<String, Object> attributes = new HashMap<String, Object>();
//        attributes.put("domain", environment.getProperty("system.base.web.domain"));
//        attributes.put("webUrl", environment.getProperty("system.base.web.url"));
//        attributes.put("staticPath", environment.getProperty("system.base.web.static.path"));
//        attributes.put("jsPath", environment.getProperty("system.base.web.js.path"));
//        attributes.put("cssPath", environment.getProperty("system.base.web.css.path"));
//        attributes.put("imagesPath", environment.getProperty("system.base.web.images.path"));

        
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/jsp/");
        bean.setSuffix(".jsp");
        bean.setContentType("text/html;charset=UTF-8");
        bean.setAttributesMap(attributes);
        return bean;
    }

    /**
     * json消息转换适配器，用于支持RequestBody、ResponseBody
     *
     * @return
     */
    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
        List<MediaType> mediaTypeList = new ArrayList<MediaType>();
        mediaTypeList.add(MediaType.APPLICATION_JSON);
        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        MappingJackson2HttpMessageConverter bean = new MappingJackson2HttpMessageConverter();
        bean.setObjectMapper(objectMapper);
        bean.setSupportedMediaTypes(mediaTypeList);
        return bean;
    }

    @Bean
    public Jackson2ObjectMapperFactoryBean jackson2ObjectMapperFactoryBean() {
        Jackson2ObjectMapperFactoryBean bean = new Jackson2ObjectMapperFactoryBean();
        bean.setIndentOutput(true);
        bean.setSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return bean;
    }

    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        configurer.mediaType("json", MediaType.APPLICATION_JSON);
    }

    /**
     * 附件Multipart解决方案
     *
     * @return
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver bean = new CommonsMultipartResolver();
        bean.setDefaultEncoding("utf-8");
        bean.setMaxUploadSize(102400000);
        return bean;
    }

    /**
     * 添加拦截器
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    	registry.addInterceptor(new DataInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**","/tempUpload/**");
        registry.addInterceptor(new SessionInterceptor()).addPathPatterns("/**").excludePathPatterns("/static/**","/tempUpload/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
        registry.addResourceHandler("/tempUpload/**").addResourceLocations("/tempUpload/");
    }
}
